/*
 * @Descripttion : Manager 构造函数（子集：imagesManger）
 * @Author       : QianFox Team
 * @Date         : 2022-03-21 09:15:18
 * @LastEditors  : QianFox Team
 * @LastEditTime : 2024-08-07 09:22:21
 */

import { dialog } from './dialog';
import { tabs } from './tabs';
import { progress } from './progress';

function Manager() {
    this.target = ''; // 触发目标DOM节点(用于返回时作判断)
    this.url = ''; // 请求地址
    this.tabsIndex = 0; // 管理器 tabs 的 index
    this.cancelText = '取消';
    this.confirmText = '确定';
    this.width = '1000px';
    this.marginTop = '10vh';
    this.buttonSize = 'small';
    this.buttonAlign = 'center';
    this.longButton = true;
    this.progress = null;
}

/**
 * @description: 传入触发目标DOM节点
 * @param {*} target
 * @return {*}
 * @Date: 2022-04-22 16:21:05
 */
Manager.prototype.show = function (target) {
    // 判断 url
    if (!this.url) {
        if (this.className === 'images-manager') {
            throw new Error(`请先初始化：foxui.imagesManager.init(...)；传入 url！`);
        } else if (this.className === 'videos-manager') {
            throw new Error(`请先初始化：foxui.videoManager.init(...)；传入 url！`);
        } else {
            throw new Error(`请先初始化！`);
        }
    }
    // 保存触发目标DOM节点
    this.target = target || '';
    // 异步获取数据
    this._ajax({}, 'init');
    // 打开管理器
    dialog({
        title: this.title,
        content: this._contentHtml(),
        cancelText: this.cancelText,
        confirmText: this.confirmText,
        width: this.width,
        marginTop: this.marginTop,
        buttonSize: this.buttonSize,
        buttonAlign: this.buttonAlign,
        longButton: this.longButton,
        className: this.className,
        confirm: this._confirm.bind(this),
    });
    // 初始化 tabs
    this.tabsIndex = 0; // 修复关闭后，再次选择图片 bug
    tabs.init($(`.${this.className} .foxui-tabs`));
    // 添加分页
    this._addPagination();
    // 初始化 添加组
    this._addGroupInit && this._addGroupInit();
    // 初始化 修改组
    this._modifyGroupInit && this._modifyGroupInit();
};

/**
 * @description: 更新 tabs
 * @param {*} index
 * @param {*} id
 * @return {*}
 * @Date: 2022-04-18 15:47:28
 */
Manager.prototype._updateTab = function ({ index, id }) {
    if (id === this.tabsId) {
        this.tabsIndex = index;
    }
};

/**
 * @description: 添加进度条
 * @return {*}
 * @Date: 2024-01-12 09:44:26
 */
Manager.prototype._addProgress = function (text) {
    let htmlArr = [
        '<div class="manager-progress">',
        '<div class="progress-chart">',
        '</div>',
        '<div class="progress-text foxui-mt-10">',
        `<p>${text || '文件'}上传中...</p>`,
        '</div>',
        '</div>',
    ];
    $(`.${this.className}`).append(htmlArr.join(''));

    this.progress = progress({
        el: `.${this.className} .progress-chart`,
        percent: 0,
        type: 'circle',
    });
};

/**
 * @description: 移除进度条
 * @return {*}
 * @Date: 2024-01-12 10:35:49
 */
Manager.prototype._removeProgress = function () {
    $(`.${this.className} .manager-progress`).remove();
    this.progress = null;
};

/**
 * @description: 添加观察者
 * @param {*}
 * @return {*}
 * @Date: 2022-04-18 17:07:40
 */
Manager.prototype._addCustomer = function () {
    tabs._onChange(this._updateTab.bind(this));
};

export { Manager };
